﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Configuration;
using System.Data.SqlClient;

/// <summary>
/// USER DETAILS (integrated with bonus table)
/// </summary>
/// 

namespace Bestlife.ERP.DataAccess
{
    public class users : System.Web.UI.Page
    {
        Double userCPV;
        String User_Status;
        Double U_bv;
        Double U_pv;
        Double U_tpv;
        Double U_sales;
        String Uid;
        SqlDataReader rdRUStatus;
       // SqlDataReader rdRStatus;


        public users()
        {
            //
            // TODO: Add constructor logic here
            //
        }// close users()


        #region After shoping cart transactions

        public void users_level(String uUid, Double uU_bv, Double uU_pv, Double uU_sales)
        {

            try
            {//try

                
                U_bv = uU_bv;
                U_pv = uU_pv;
                U_sales = uU_sales;
                Uid = uUid;


                                try // select user status
                                {
                                    String connStr = ConfigurationManager.ConnectionStrings["MainConnStr"].ConnectionString;
                                    Bestlife.ERP.DataAccess.clsCon.connect(connStr);
                                    String sqlUstatus = "SELECT user_Status from Bonus where User_id='" + Uid + "'";
                                    rdRUStatus = clsCon.ReadTable(sqlUstatus);

                                    if (rdRUStatus.Read())
                                    {
                                        User_Status = rdRUStatus["user_Status"].ToString();
                                    }

                                }   // close try


                                catch (Exception err)// select user status
                                {

                                    Response.Redirect("404.aspx?Massage=" + err.Message.ToString() + "user Status error");
                                }

                                rdRUStatus.Close();



                if (User_Status == "2")
                {

                            try
                            {
                                BusinessRules._5presnter pp5 = new BusinessRules._5presnter();
                                pp5.presentercal_5(Uid, U_bv, U_pv, U_sales, User_Status);
                            }

                            catch (Exception err)
                            {

                                Response.Redirect("404.aspx?Massage=" + err.Message.ToString() + "error - Assign value to presental5");
                            }

                }


                else if (User_Status == "3")
                {


                            try
                            {
                                BusinessRules._10_presenter p10 = new BusinessRules._10_presenter();
                                p10.presentercal_10(Uid, U_bv, U_pv, U_sales, User_Status);
                            }

                            catch (Exception err)
                            {

                                Response.Redirect("404.aspx?Massage=" + err.Message.ToString() + "error - Assign value to 10Presenter");
                            }


                }



                else if (User_Status == "4")

                {

                            try
                            {
                                BusinessRules._15_presenter p15 = new BusinessRules._15_presenter();
                                p15.presentercal_15(Uid, U_bv, U_pv, U_sales, User_Status);
                            }
                            catch (Exception err)
                            {

                                Response.Redirect("404.aspx?Massage=" + err.Message.ToString() + "error - Assign value to 15_presenter");
                            }



                }

                else if (User_Status == "5")
                {

                            try
                            {

                                BusinessRules.Agent agt = new BusinessRules.Agent();
                                agt.agent(Uid, U_bv, U_pv, U_sales, User_Status);
                            }

                            catch (Exception err)
                            {

                                Response.Redirect("404.aspx?Massage=" + err.Message.ToString() + "error - Assign value to Agent");
                            }
                            

                             

                }

                else if (User_Status == "6")
                {
                            try
                            {

                                BusinessRules.Ruby r1 = new BusinessRules.Ruby();
                                r1.Rubys(Uid, U_bv, U_pv, U_sales, User_Status);
                            }

                            catch (Exception err)
                            {

                                Response.Redirect("404.aspx?Massage=" + err.Message.ToString() + "error - Assign value to Ruby");
                            }

                }

                else if (User_Status == "7")
                {
                    
                            try
                            {

                                BusinessRules.Diamond d1 = new BusinessRules.Diamond();
                                d1.Diamonds(Uid, U_bv, U_pv, U_sales, User_Status);
                            }

                            catch (Exception err)
                            {

                                Response.Redirect("404.aspx?Massage=" + err.Message.ToString() + "error - Assign value to Diamond");
                            }

                }

                else if (User_Status == "8")

                {
                            try
                            {

                                BusinessRules.Senior_Diamond sd = new BusinessRules.Senior_Diamond();
                                sd.S_Diamonds(Uid, U_bv, U_pv, U_sales, User_Status);
                            }

                            catch (Exception err)
                            {

                                Response.Redirect("404.aspx?Massage=" + err.Message.ToString() + "error - Assign value to Senior Diamond");
                            }

                }


                else if (User_Status == "9")
                {

                                try
                                {

                                    BusinessRules.Senior_EXE_Diamond sed = new BusinessRules.Senior_EXE_Diamond();
                                    sed.SE_Diamonds(Uid, U_bv, U_pv, U_sales, User_Status);
                                }

                                catch (Exception err)
                                {

                                    Response.Redirect("404.aspx?Massage=" + err.Message.ToString() + "error - Assign value to Senior Executive Diamond");
                                }

                }

                else if (User_Status == "10")
                {
                                try
                                {

                                    BusinessRules.Crown_Diamond cd = new BusinessRules.Crown_Diamond();
                                    cd.Crown_Diamonds(Uid, U_bv, U_pv, U_sales, User_Status);
                                }

                                catch (Exception err)
                                {

                                    Response.Redirect("404.aspx?Massage=" + err.Message.ToString() + "error - Assign value to Senior Crown Diamond");
                                }

                }

                else if (User_Status == "11")
                {

                               // 2 crown diamond

                }

                else if (User_Status == "12")
                {
                              //3 crown diamond

                }

                else if (User_Status == "13")
                {
                            // 4 crown diamond

                }
                else if (User_Status == "14")
                {
                            // 5 crown diamond

                }

               




            }//try


            catch (Exception err)
            {

                Response.Redirect("404.aspx?Massage=" + err.Message.ToString());
            }

            finally
            {
                clsCon.conn_new.Close();
                rdRUStatus.Close();

            }   //finally

            //return User_Status;
            ///// Redirect bonus type class
            
        }

        #endregion


        # region Get USER CPV

        public Double users_cpv(String id)
        {

            try
            {//try

                String connStr = ConfigurationManager.ConnectionStrings["MainConnStr"].ConnectionString;
                Bestlife.ERP.DataAccess.clsCon.connect(connStr);


                String sqlstatus = "SELECT Cpv from Bonus where User_id='" + id + "'";

                rdRUStatus = clsCon.ReadTable(sqlstatus);

                if (rdRUStatus.Read())
                {
                    String AAA = rdRUStatus["Cpv"].ToString();
                    userCPV = double.Parse(AAA);

                }




            }//try


            catch (Exception err)
            {

                Response.Redirect("404.aspx?Massage=" + err.Message.ToString());
            }

            finally
            {
                clsCon.conn_new.Close();
                rdRUStatus.Close();
                

            }   //finally


            return userCPV;



        }

        #endregion

        # region UpdateTotal commisions

        public void UpdateParent(String id, Double tcom)
        {

            try
            {

                String connStr = ConfigurationManager.ConnectionStrings["MainConnStr"].ConnectionString;
                Bestlife.ERP.DataAccess.clsCon.connect(connStr);

                SqlCommand act = new SqlCommand("UPDATE Bonus SET T_commision='" + tcom + "',Last_Modify='" + System.DateTime.Now + "' WHERE User_id='" + id + "'", clsCon.conn_new);
                act.ExecuteNonQuery();
            }

            catch (Exception err)
            {

                Response.Redirect("404.aspx?Massage=" + err.Message.ToString());
            }

            finally
            {
                clsCon.conn_new.Close();
            }

        }



        #endregion

        //# region Update Total cpv(Parent)

        //public void UpdateParent(String id, Double tcpv)
        //{

        //    try
        //    {

        //        String connStr = ConfigurationManager.ConnectionStrings["MainConnStr"].ConnectionString;
        //        Bestlife.ERP.DataAccess.clsCon.connect(connStr);

        //        SqlCommand act = new SqlCommand("UPDATE Bonus SET Cpv='" + tcpv + "' WHERE User_id='" + id + "'", clsCon.conn_new);

        //        act.ExecuteNonQuery();
        //    }
        //    catch (Exception Ex)
        //    {

        //        //Response.Redirect("Massages.aspx?Massage=" + Ex.Message.ToString());
        //    }
        //    finally
        //    {
        //        clsCon.conn_new.Close();
        //    }

        //}


        //#endregion

        # region Get USER ID

        public string GetUserId(String uname)
        {
            string Uid = "";
            try
            {//try

                String connStr = ConfigurationManager.ConnectionStrings["MainConnStr"].ConnectionString;
                Bestlife.ERP.DataAccess.clsCon.connect(connStr);


                String sqlstatus = "SELECT Use_ID from Tbl_User where Use_Name='" + uname + "'";
                //SqlDataReader rdRStatus;
                rdRUStatus = clsCon.ReadTable(sqlstatus);


                if (rdRUStatus.Read())
                {
                    Uid = rdRUStatus["Use_ID"].ToString();
                }


            }//try


            catch (Exception err)
            {

                Response.Redirect("404.aspx?Massage=" + err.Message.ToString());
            }  //catch

            finally
            {
                clsCon.conn_new.Close();
                rdRUStatus.Close();

            }   //finally
            return Uid;



        }

        #endregion

        # region check USER Table

        public Boolean GetUserTablest(String uid)
        {
            bool stBool = false;
            try
            {//try

                String connStr = ConfigurationManager.ConnectionStrings["MainConnStr"].ConnectionString;
                Bestlife.ERP.DataAccess.clsCon.connect(connStr);


                String sqlstatus = "SELECT user_Status from Bonus where User_id='" + uid + "'";

                rdRUStatus = clsCon.ReadTable(sqlstatus);


                if (rdRUStatus.Read())
                {
                    stBool = true;
                }


            }//try


            catch (Exception err)
            {

                Response.Redirect("404.aspx?Massage=" + err.Message.ToString());
            }

            finally
            {
                clsCon.conn_new.Close();
                rdRUStatus.Close();

            }   //finally
            return stBool;



        }



        #endregion

        # region Update Bonus By Cart (By chaturanga)

        public void UpdateBonus(String uid, Double totPrice, Double totPV, Double totBV,Double pv,Double bv)
        {  
            Bestlife.ERP.DataAccess.D_bonus dbns = new D_bonus();
            Bestlife.ERP.Entities.E_Bonus ebns = new Entities.E_Bonus();

            ebns = dbns.Getbonus(uid);

            Double AllproductPv=pv;
            Double AllproductBv = bv;


            double total_Bv_Withdb = double.Parse(ebns.Bv.ToString()) + AllproductBv;
            double total_Pv_Withdb = double.Parse(ebns.Ppv.ToString()) + AllproductPv;
            double totalCpv_withdb = double.Parse(ebns.Cpv.ToString()) + AllproductPv;
            double totalCpv_monthwithdb = double.Parse(ebns.CPV_month.ToString()) + AllproductPv;
            double totaltppv_monthwithDB = double.Parse(ebns.Tppv_month.ToString())+AllproductPv;
            double totaltbv_monthwithDB = double.Parse(ebns.Tbv_month.ToString()) + AllproductBv;

            try
            {
                //SET Cpv='" + Total_cpv + "',CPV_month='" + Tot_monthCPV + "',tppv_month='" + tppv_month + "',tbv_month='" + tppv_month + "',
                String connStr = ConfigurationManager.ConnectionStrings["MainConnStr"].ConnectionString;
                Bestlife.ERP.DataAccess.clsCon.connect(connStr);

                SqlCommand act = new SqlCommand("UPDATE Bonus SET Cpv='" + totalCpv_withdb + "',tppv_month='" + totaltppv_monthwithDB + "',tbv_month='" + totaltbv_monthwithDB + "',CPV_month='" + totalCpv_monthwithdb + "',Tpv='" + totPV + "',ppv='" + total_Pv_Withdb + "',T_sales='" + totPrice + "',Bv='" + total_Bv_Withdb + "',Tbv='" + totBV + "' WHERE User_id='" + uid + "'", clsCon.conn_new);
                act.ExecuteNonQuery();
                //End shopping cart purchase
            }
            catch (Exception err)
            {
              Response.Redirect("404.aspx?Massage=" + err.Message.ToString());
            }
            finally
            {
                clsCon.conn_new.Close();
            }

        }



        #endregion


    } //close public users
}//Close namespaces